package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.base.core.CoreServer;
import com.frame.work.common.constant.Const;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JedisUtil;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.TblFingerprint;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.mapper.TblFingerprintMapper;
import com.frame.work.service.TblSecurityLogService;
import com.frame.work.service.impl.JpushService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_CONTROL;
import static com.frame.work.common.constant.CallType.TYPE_UP;
import static java.lang.Integer.parseInt;
/**
 *指纹锁 聪明屋
 */
@Data
public class ZhiWenSuo_CongMingWu_0FBE0202 extends BaseFrame {

    public ZhiWenSuo_CongMingWu_0FBE0202(String gatewayMac, String data,String type) {

        //将基础数据进行处理
        super(gatewayMac, data);
        //根据不同类型进行判断
        if(TYPE_CONTROL.equals(type)){
            ZhiWenSuo_CongMingWu_0FBE0202_control(gatewayMac,data);
        }else if (TYPE_UP.equals(type)){
            ZhiWenSuo_CongMingWu_0FBE0202_up(gatewayMac,data);
        }



    }
    public void ZhiWenSuo_CongMingWu_0FBE0202_up(String gatewayMac, String data) {
        //处理此设备特有的数据

        String reportType = StringUtils.substring(data, 40, 42);
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", reportType);
        this.setActions(actions);
        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }
    public void ZhiWenSuo_CongMingWu_0FBE0202_control(String gatewayMac, String data) {

        //将基础数据进行处理

        this.setDeviceType(StringUtils.substring(data, 22, 24));
        this.setProductType(StringUtils.substring(data, 24, 26));

        String eventType = StringUtils.substring(data, 36, 40);
        String content = "";
        String userId = StringUtils.substring(data, 42, 46);


        //获取mapper进行数据库操作
        SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);

        userId = Integer.toString(parseInt(userId, 16));
        List<Map> list = testMapper.getDevice4(userId, this.getDeviceMac());
        if (list.size() > 0) {
            String deviceName = (String) list.get(0).get("device_name");

            String homeName = "";
            String ls_text = "";
            String type = TypeEnum.NORMAL.code();
            if (list.get(0).get("home_name") != null && list.get(0).get("home_name") != "") {
                homeName = (String) list.get(0).get("home_name");
            }
            if ("A153".equals(eventType)) {
                content = homeName + deviceName + "通过APP远程打开";
            } else if ("A151".equals(eventType)) {
                content = homeName + deviceName + "通过密码打开";
            } else if ("A150".equals(eventType)) {
                if (list.get(0).get("fingerprint_name") == "" || list.get(0).get("fingerprint_name") == null) {
                    String idType = "01";
                    if (parseInt(userId, 16) > 5) {
                        idType = "02";
                    }
                    //指纹锁表里新增数据
                    TblFingerprint tblFingerprint = new TblFingerprint();
                    tblFingerprint.setDeviceId((String) list.get(0).get("device_id"));
                    tblFingerprint.setNumber(Integer.toString(parseInt(userId, 16)));
                    tblFingerprint.setName("指纹" + Integer.toString(parseInt(userId, 16)));
                    tblFingerprint.setIdType(idType);

                    TblFingerprintMapper tblFingerprintMapper = (TblFingerprintMapper) SpringUtil.getBean(TblFingerprintMapper.class);
                    tblFingerprintMapper.insert(tblFingerprint);


                    content = homeName + deviceName + "被指纹" + parseInt(userId, 16) + "打开";
                } else {
                    content = homeName + deviceName + '被' + (String) list.get(0).get("fingerprint_name") + "打开";
                }
            } else if ("A152".equals(eventType)) {
                content = homeName + deviceName + "通过门卡打开";
            }


            /**
             * 云片短信
             */
            /**
             * 极光推送
             */
            type = TypeEnum.NORMAL.code();

            String orgCode = (String) list.get(0).get("organization_code");
            String userId2 = (String) list.get(0).get("user_id");
            String deviceId = (String) list.get(0).get("device_id");
            String sourceId = (String) list.get(0).get("source_id");
            String account = (String) list.get(0).get("account");
            String nickName = (String) list.get(0).get("nick_name");
            String sourceType = Long.toString((long) list.get(0).get("source_type"));
            //极光推送部分（代码存在残缺）


            if (list.get(0).get("source_id") != null && !"".equals(list.get(0).get("source_id"))) {
                String pushType = "1";
                String defendStatus = "02";

                String pushType1 = (String) list.get(0).get("pushType");
                String defend_status = (String) list.get(0).get("defend_status");
                if (pushType1 != null && !"".equals(pushType1)) {
                    pushType = (String) list.get(0).get("pushType");
                }
                if (defend_status != null && !"".equals(defend_status)) {
                    defendStatus = (String) list.get(0).get("defend_status");
                }

                if ( "1".equals(pushType) || ("2".equals(pushType) && "01".equals(defendStatus)) || ("3".equals(pushType) && "02".equals(defendStatus))) {
                           /* var extrasObj = {
                                    'type': Const.EXTRAS_CODE_ALARM_DEVICE,
                                    'content': list.get(0).get.device_id
                                    };*/
                    JpushService jPushService =SpringUtil.getBean(JpushService.class);
                    try {
                        jPushService.sendNoSave("", content, type, sourceId, Const.MsgType.MSG_WARNING_TYPE, deviceId, userId2, sourceType, orgCode);

                    } catch (Exception e) {

                    }

                }
            }
            //保存报警信息
            TblSecurityLog tblSecurityLog = new TblSecurityLog();
            tblSecurityLog.setGatewayMac(this.getGatewayMac());
            tblSecurityLog.setDeviceMac(this.getDeviceMac());
            tblSecurityLog.setContent(content);
            tblSecurityLog.setType(type);
            //获取userId
            String userId1 = (String) list.get(0).get("user_id");
            tblSecurityLog.setUserId(userId1);
            TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
            tblSecurityLogService.insert(tblSecurityLog);
            //场景触发

            Integer sceneCode = (Integer) list.get(0).get("scene_code");
            if (("A150".equals(eventType)) && sceneCode != null ) { // 场景不为空
                // 场景触发
                CoreServer coreServer = (CoreServer) SpringUtil.getBean(CoreServer.class);
                JedisUtil jedisUtil = (JedisUtil) SpringUtil.getBean(JedisUtil.class);
                Jedis jedis = jedisUtil.getJedis();
//                coreServer.writeDataToGateWay(gatewayMac, FrameMaker.getWriteGateway50SceneTrigger09Frame(gatewayMac, sceneCode),jedis);
            }

        }


        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("eventType", eventType);
        actions.put("content", content);


        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }

}
